<template>
  <div class="pageOut">
    <div class="pageIn">
      <el-form :inline="true" size="small">
        <el-form-item>
          <BackButton pages="/GoodsList" />
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="handleAdd"
            v-show="permission.add"
          >新增口味</el-button>
        </el-form-item>
      </el-form>
      <el-table
        border
        ref="table"
        :height="height-170"
        :data="tableData"
        :header-cell-style="{background:'#FAFAFA',color:'#606266'}"
        v-loading="loading"
        element-loading-background="rgba(255, 255, 255, 0.5)"
        style="width: 100%"
      >
        <el-table-column prop="id" width="100" label="序号"></el-table-column>
        <el-table-column prop="taste_name" label="菜品口味名称"></el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>

        <el-table-column fixed="right" label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(scope.row)"
              v-show="permission.edit"
            >修改</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="handleDel(scope.row.id)"
              v-show="permission.del"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>

      <!-- 商家添加和商家编辑 -->
      <el-dialog
        width="500px"
        :title="type==='add'?'添加口味':'修改口味'"
        :visible.sync="visible"
        @close="onCancel('ruleForm')"
        custom-class="custom_drawer"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          size="mini"
          label-width="80px"
          class="form-drawer__main"
        >
          <el-form-item label="口味" prop="taste_id" style="margin-bottom:30px;">
            <el-select
              v-model="ruleForm.taste_id"
              clearable
              filterable
              placeholder="请选择口味"
              style="width:250px;"
            >
              <el-option
                v-for="(item,index) in taste"
                :key="index"
                :label="item.taste_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="onCancel('ruleForm')" size="small">取 消</el-button>
          <el-button
            type="primary"
            size="small"
            @click="onConfirm('ruleForm')"
            :loading="confirmLoading"
          >{{ confirmLoading ? '提交中 ...' : '确 定' }}</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  MAIN
} from "../../utils/api";
import BackButton from "../../components/BackButton";
export default {
  name: "MenuTastes",
  props: ["height"],

  data() {
    return {
      tableData: [],
      menu_id: "", //路由传值 菜品id
      merchant_id: "", //路由传值 商家id
      tooltip: true, //内容过长被隐藏时显示 tooltip
      loading: true, //显示加载表格
      pageSize: 10, //每页多少条
      currentPage: 1, //当前第几页
      pageSizes: [10, 20, 30, 40, 50], //每页选择条数
      total: 0, //共多少条
      errImg: 'this.src="' + require("../../../static/img/user.png") + '"',
      permission: {
        add: false,
        edit: false,
        del: false
      }, //权限
      //编辑弹窗
      visible: false,
      confirmLoading: false,
      type: "",
      currentRow: {}, //当前行数据信息
      ruleForm: {
        taste_id: ""
      }, //添加和修改的表单
      rules: {
        taste_id: [{ required: true, message: "请选择口味", trigger: "change" }]
      },
      taste: {}
    };
  },
  components: {
    BackButton
  },
  created() {
    this.menu_id = this.$route.query.menu_id;
    this.merchant_id = this.$route.query.merchant_id;
  },
  mounted() {
    this.getResource();
  },
  methods: {
    //获取权限资源
    getResource() {
      // console.log(this.$route.name);
      this.loading = true;
      const userinfo = JSON.parse(sessionStorage.getItem('userInfo')); //解密,获取用户登录信息
      let arr = userinfo.menu_id.split(",");
      // console.log(arr)
      // 新增菜品的口味 30
      // 修改菜品的口味 31
      // 删除菜品的口味 32
      if (arr.indexOf("30") > -1) {
        this.permission.add = true;
      }
      if (arr.indexOf("31") > -1) {
        this.permission.edit = true;
      }

      if (arr.indexOf("32") > -1) {
        this.permission.del = true;
      }


      this.getData();
    },
    //清除输入框负数
    clearMinus(e, type) {
      let boolean = new RegExp("^[0-9][0-9.]*$").test(e.target.value);
      if (!boolean) {
        e.target.value = "";
        if (type === "price") this.ruleForm.price = "";
      }
    },
    
    //获取初始数据
    getData() {
      // this.loading = true;
      // let params = {
      //   menu_id: this.menu_id,
      //   pageNumber: this.currentPage,
      //   pageSize: this.pageSize
      // };
      // this.postRequest(menuTastes, params, MAIN).then(res => {
      //   if (res.code == 200) {
      //     //   console.log(res);
      //     this.tableData = res.data.list;
      //     this.total = res.data.count;
      //     this.loading = false;
      //     this.$nextTick(() => {
      //       //避免表格表头样式错乱
      //       if (this.$refs.table) this.$refs.table.doLayout();
      //     });
      //   }
      // });
    },

    //分页
    handleSizeChange(val) {
      //每页 ${val} 条
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      //当前页
      this.currentPage = val;
      this.getData();
    },
    //新增
    handleAdd() {
      this.type = "add";
      this.currentRow = {};
      this.tasteSelects();
      this.visible = true;
    },
    //编辑
    handleEdit(row) {
      this.type = "edit";
      this.currentRow = row;
      this.tasteSelects();
      this.visible = true;
      this.ruleForm = {
        taste_id: row.taste_id
      };
    },
    //删除
    handleDel(id) {
      let parm = {
        id: id
      };
      // this.$confirm("确认删除?", "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   type: "warning"
      // }).then(() => {
      //   this.postRequest(menuTasteDel, parm, MAIN).then(res => {
      //     // console.log(res);
      //     if (res.code == 200) {
      //       this.$notify({
      //         title: "删除成功",
      //         message: res.msg,
      //         type: "success"
      //       });
      //       this.getData();
      //     }
      //   });
      // });
    },

    onConfirm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.confirmLoading = true;
          // setTimeout(() => this.onCancel(formName), 1000);
          if (this.type == "add") {
            this.menuTasteAdd();
          } else if (this.type == "edit") {
            this.menuTasteEdit();
          }
        } else {
          return false;
        }
      });
    },
    // 添加
    menuTasteAdd() {
      let params = {
        taste_id: this.ruleForm.taste_id,
        menu_id: this.menu_id
      };

      this.postRequest(menuTasteAdd, params, MAIN).then(res => {
        // console.log(res);
        if (res.code == 200) {
          this.$notify({
            title: "添加成功",
            message: res.msg,
            type: "success"
          });
        }
        this.onReset();
        this.getData();
      });
    },
    // 编辑
    menuTasteEdit() {
      let params = {
        id: this.currentRow.id,
        taste_id: this.ruleForm.taste_id,
        menu_id: this.menu_id
      };

      // this.postRequest(menuTasteEdit, params, MAIN).then(res => {
      //   // console.log(res);
      //   if (res.code == 200) {
      //     this.$notify({
      //       title: "编辑成功",
      //       message: res.msg,
      //       type: "success"
      //     });
      //   }
      //   this.onReset();
      //   this.getData();
      // });
    },
    onCancel(formName) {
      this.$refs[formName].resetFields();
      this.onReset();
    },
    //清空重置表单
    onReset() {
      this.visible = false;
      this.confirmLoading = false;
      this.ruleForm = {
        sku_name: "",
        merchant_id: ""
      };
    }
  }
};
</script>

<style scoped>
/deep/ .el-input-group__append,
.el-input-group__prepend {
  background-color: #1890ff;
  color: #fff;
  cursor: pointer;
}
/deep/ .el-input--mini .el-input__inner {
  height: 32px;
  line-height: 32px;
}

/deep/ input::-webkit-outer-spin-button,
/deep/ input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
/deep/ input[type="number"] {
  -moz-appearance: textfield;
}
</style>
